package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Customer;
import org.example.entity.Roles;
import org.example.models.R;
import org.example.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;


    @GetMapping
    public R list(){
        return R.success(customerService.list());
    }

    @GetMapping("/page")
    public R<Page<Customer>> page(
            @RequestParam(name="current",defaultValue = "1") int current,
            @RequestParam(name="size",defaultValue = "2") int size,
            @RequestParam(name="name",required = false) String name
    ){
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();//查询条件
        if(name != null && name.length() > 0){
            queryWrapper.lambda().like(Customer::getName,name);
        }
        Page<Customer> page = new Page<>(current,size);
        customerService.page(page,queryWrapper);
        return R.success(page);
    }

    @PostMapping
    public R save(@RequestBody Customer customer){
        return R.success(customerService.save(customer));
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        return R.success(customerService.removeById(id));
    }
    @PutMapping
    public R update(@RequestBody Customer customer){
        return R.success(customerService.updateById(customer));
    }
}
